﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


/// <summary>
/// Summary description for BanDoProvider
/// </summary>
namespace EHTWebsite.Code
{
    public class BanDoProvider
    {
        /// <summary>
        /// CuongBH
        /// 07/12/2011
        /// </summary>
        /// <param name="theBanDo"> Đối tượng bản đồ</param>
        /// <param name="IsAdd">Biến kiểm tra xem là insert hay update</param>
        public static Int32 IUBanDo(BanDo theBanDo, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUBanDos";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter theBanDoIDParameter = new SqlParameter();

            theBanDoIDParameter.ParameterName = "@IDOut";
            theBanDoIDParameter.Direction = ParameterDirection.Output;
            theBanDoIDParameter.DbType = DbType.Int32;

            command.Parameters.Add(theBanDoIDParameter);
            command.Parameters.Add(new SqlParameter("@ID", theBanDo.ID));
            command.Parameters.Add(new SqlParameter("@ToaDo", theBanDo.ToaDo));
            command.Parameters.Add(new SqlParameter("@MoTaVN", theBanDo.MoTaVN));
            command.Parameters.Add(new SqlParameter("@MoTaOther", theBanDo.MoTaOther));
            command.Parameters.Add(new SqlParameter("@Publish", theBanDo.Publish));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theBanDo.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@IDOut"].Value.ToString());
        }

        /// <summary>
        /// Xoá thông tin bản đồ
        /// </summary>
        /// <param name="ID">ID</param>
        public static void DeleteBanDoByID(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteBanDoByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetBanDosForBackEnd(Int32 pageIndex, Int32 pageSize,out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetBanDosForBackEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        public static DataSet GetBanDosForFrontEnd(Boolean isVN)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetBanDosForFrontEnd";
            command.Parameters.Add(new SqlParameter("@IsVN", isVN));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        public static BanDo GetBanDoByID(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetBanDoByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            BanDo theBanDo = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theBanDo = new BanDo();
                theBanDo.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
                theBanDo.ToaDo = ds.Tables[0].Rows[0]["ToaDo"].ToString();
                theBanDo.MoTaVN = ds.Tables[0].Rows[0]["MoTaVN"].ToString();
                theBanDo.MoTaOther = ds.Tables[0].Rows[0]["MoTaOther"].ToString();
                theBanDo.NumberOfOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
                theBanDo.ID = Int32.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
            }

            return theBanDo;
        }

        public static void ChangePublishByBanDoID(Int32 banDoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByBanDoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", banDoID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateNumberOfOrderByBanDoID(Int32 banDoID, Int32 numberOfOrder)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNumberOfOrderByBanDoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", banDoID));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", numberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
        }
    }
}